<template>
  <web-view :src="src"></web-view>
</template>
<script setup>
import {onLoad, onShow} from "@dcloudio/uni-app";
import { reactive, ref, onMounted } from "vue";
import { ObjectToQueryString } from "@/utils/utils";

const src = ref("");
const locationInfo = reactive({ latitude: "", longitude: "" })
const queryParams = ref({})
const init = ref(false)
const sceneObj = reactive({ scene: "", wx_scene: 0 })

onLoad(async (options) => {
  const { q } = options
  if (!q) return uni.showToast({ title: '参数错误', icon: 'none' })
  queryParams.value = options
  sceneObj.scene = options?.scene || ""
  sceneObj.wx_scene = uni.getLaunchOptionsSync().scene
})

onShow(() => {
  if (!src.value && init.value) handleInit()
})

onMounted(() => {
  handleInit()
  init.value = true
})

const handleInit = async () => {
  await getApp().getUserLoginInfo()
  const user = await getApp().getUserInfo()
  // 是否登录注册
  if (
    getApp().globalData.user.phone === null ||
    getApp().globalData.user.phone === ""
  ) {
    await uni.navigateTo({ url: "/pages/vip/register?tag=scanCode" });
    return
  }
  const { q } = queryParams.value
  const queryPrams = ObjectToQueryString({
    'accesstoken': uni.getStorageSync("TOKEN"),
    'openid': uni.getStorageSync("OPENID"),
    'unionid': uni.getStorageSync("UNIONID"),
    'memberid': user?.memberScore?.memberId || '',
    ...sceneObj
  })
  const firstUrl = decodeURIComponent(q)
  src.value = `${firstUrl}${firstUrl.includes('?') ? '&' : '?' }${queryPrams}`
}
</script>

<style></style>
